# -*- coding: utf-8 -*-

#导入必要的工具包
import pandas as pd
import numpy as np
from sklearn.cluster import MiniBatchKMeans
from sklearn.model_selection import train_test_split
from sklearn import metrics

from sklearn.decomposition import PCA
import time

import matplotlib.pyplot as plt

train = pd.read_csv('./train_events.csv')

labels = ["event_id","user_id","start_time","city","state","zip","country","lat","lng"]

n_trains = 10000
y_train = train["event_id"].values[:n_trains]
X_train = train.drop(labels,axis=1).values[:n_trains]

# print train.info()
# print train.head(1)
#
# print y_train

from sklearn.decomposition import PCA
# pca = PCA(n_components=0.75)
# pca.fit(X_train)
#
# X_train_pca = pca.transform(X_train)
#
# # 降维后的特征维数
print(X_train.shape)
# print(X_train_pca.shape)

# n_trains = 1000
# y_train = train.label.values[:n_trains]
# X_train = train.drop("label",axis=1).values[:n_trains]

X_train_part, X_val, y_train_part, y_val = train_test_split(X_train,y_train, train_size = 0.8,random_state = 0)
print(X_train_part.shape)
print(X_val.shape)


def K_cluster_analysis(K, X_train, y_train, X_val, y_val):
    start = time.time()

    print("K-means begin with clusters: {}".format(K));

    # K-means,在训练集上训练
    mb_kmeans = MiniBatchKMeans(n_clusters=K)
    mb_kmeans.fit(X_train)

    # 在训练集和测试集上测试
    # y_train_pred = mb_kmeans.fit_predict(X_train)
    y_val_pred = mb_kmeans.predict(X_val)

    # 以前两维特征打印训练数据的分类结果
    # plt.scatter(X_train[:, 0], X_train[:, 1], c=y_pred)
    # plt.show()

    # K值的评估标准
    # 常见的方法有轮廓系数Silhouette Coefficient和Calinski-Harabasz Index
    # 这两个分数值越大则聚类效果越好
    # CH_score = metrics.calinski_harabaz_score(X_train,mb_kmeans.predict(X_train))
    CH_score = metrics.silhouette_score(X_train, mb_kmeans.predict(X_train))

    # 也可以在校验集上评估K
    v_score = metrics.v_measure_score(y_val, y_val_pred)

    end = time.time()
    print("CH_score: {}, time elaps:{}".format(CH_score, int(end - start)))
    print("v_score: {}".format(v_score))

    return CH_score, v_score


Ks = np.arange(10, 100, 10)
CH_scores = []
v_scores = []
for K in Ks:
    ch,v = K_cluster_analysis(K, X_train_part, y_train_part, X_val, y_val)
    CH_scores.append(ch)
    v_scores.append(v)

plt.plot(Ks, np.array(CH_scores), 'b-')
plt.plot(Ks, np.array(v_scores), 'g-')